﻿using PPSMS_Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PPSMS_DAL;

public class InverterStatistics_DAL : ICRUD_DAL<InverterStatistics, long>
{

    /// <summary>
    /// 删除指定ID的记录
    /// </summary>
    /// <param name="ids">逆变器统计数据表id数组</param>
    public void DeleteByIds(params long[] ids)
    {
        string sql = $"delete from INVERTER_STATISTICS where Id in ({string.Join(",", ids)})";
        DBHelper.ExecuteNonQuery(sql);
    }

    /// <summary>
    /// 获取当前最大ID值
    /// </summary>
    /// <returns>ID</returns>
    public long GetCurrentValue()
    {
        string sql = "select rowid from INVERTER_STATISTICS";
        return DBHelper.ExecuteQuery(sql, DBHelper.ExecuteScalarToInt, new object[0]);
    }

    /// <summary>
    /// 新增记录
    /// </summary>
    /// <param name="t">逆变器统计数据表对象</param>
    public void Insert(InverterStatistics t)
    {
        string sql = "insert into INVERTER_STATISTICS (INVERTERID,DATE,DAILY_ENERGY,CUMULATIVE_ENERGY,CO2_REDUCTION) values(?,?,?,?,?)";
        DBHelper.ExecuteNonQuery(sql, t.InverterId, t.Date, t.DailyEnergy, t.CumulativeEnergy, t.CO2Reduction);
    }

    /// <summary>
    /// 将一个 DataRow 对象转换为对应的 InverterStatistics 对象
    /// </summary>
    /// <param name="dr"></param>
    /// <returns>逆变器统计数据表对象</returns>
    public InverterStatistics ResultHandler(DataRow dr)
    {
        return new InverterStatistics
        {
            InverterId = dr.Field<long>("INVERTERID"),
            Date = dr.Field<string>("DATE"),
            DailyEnergy = dr.Field<double>("DAILY_ENERGY"),
            CumulativeEnergy = dr.Field<double>("CUMULATIVE_ENERGY"),
            CO2Reduction = dr.Field<double>("CO2_REDUCTION")
        };
    }

    /// <summary>
    /// 将一个 DataTable 对象转换为对应的 List<InverterStatistics> 对象
    /// </summary>
    /// <param name="dr"></param>
    /// <returns>一个包含所有逆变器统计数据表信息的List</returns>
    public List<InverterStatistics> ResultHandlerList(DataTable dr)
    {
        List<InverterStatistics> list = new List<InverterStatistics>();
        foreach (DataRow row in dr.Rows)
        {
            list.Add(ResultHandler(row));
        }
        return list;
    }

    /// <summary>
    /// 获取所有逆变器统计数据表的记录
    /// </summary>
    /// <returns>一个包含所有逆变器统计数据表信息的List</returns>
    public List<InverterStatistics> SelectAll()
    {
        string sql = "select * from INVERTER_STATISTICS";
        return DBHelper.ExecuteQuery(sql, ResultHandler, new object[0]);
    }
    public List<InverterStatistics> Selectid(long id)
    {
        string sql = $"select * from INVERTER_STATISTICS where  InverterId={id}";
        return DBHelper.ExecuteQuery(sql, ResultHandler, new object[0]);
    }

    /// <summary>
    /// 根据日期获取指定ID的逆变器统计数据表的记录
    /// </summary>
    /// <param name="date"></param>
    /// <param name="Id"></param>
    /// <returns></returns>
    public List<InverterStatistics> SelectAllByDate(string date, int Id)
    {
        string sql = $"select * from INVERTER_STATISTICS";
        sql += $" where INVERTERID = '{Id}' and DATE like '{date}%' ORDER BY date ASC";
        return DBHelper.ExecuteQuery(sql, ResultHandler, new object[0]);
    }
    /// <summary>
    /// 根据ID获取逆变器统计数据表的记录
    /// </summary>
    /// <param name="id">ID</param>
    /// <returns>逆变器统计数据表对象</returns>
    public InverterStatistics? SelectById(long id)
    {
        return SelectAll().FirstOrDefault(x => x.Id == id);
    }

    /// <summary>
    /// 根据条件修改逆变器统计数据表的记录
    /// </summary>
    /// <param name="t">新的逆变器统计数据表对象</param>
    /// <param name="id">旧的ID</param>
    /// <exception cref="ArgumentException"></exception>
    public void UpdateById(InverterStatistics t, long id)
    {
        string sql = "updata INVERTER_STATISTICS set ";
        var list = new List<object>();
        if (t.InverterId != null)
        {
            sql += "INVERTERID = ?, ";
            list.Add(t.InverterId);
        }
        if (!string.IsNullOrEmpty(t.Date))
        {
            sql += "DATE = ?, ";
            list.Add(t.Date);
        }
        if (t.DailyEnergy != null)
        {
            sql += "DAILY_ENERGY = ?, ";
            list.Add(t.DailyEnergy);
        }
        if (t.CumulativeEnergy != null)
        {
            sql += "CUMULATIVE_ENERGY = ?, ";
            list.Add(t.CumulativeEnergy);
        }
        if (t.CO2Reduction != null)
        {
            sql += "CO2_REDUCTION = ?,";
            list.Add(t.CO2Reduction);
        }
        sql = sql.TrimEnd(',');
        if (list.Count == 0) throw new ArgumentException("没有任何属性需要更新");
        sql += " where ID = ?";
        list.Add(id);
        DBHelper.ExecuteNonQuery(sql, list.ToArray());
    }

    public InverterStatistics? SelectByIdDate(long id, string date)
    {
        string sql = $"SELECT * FROM INVERTER_STATISTICS where INVERTERID ={id} AND DATE like '{date}%'";
        return DBHelper.ExecuteQuery(sql, ResultHandler, new object[0]).FirstOrDefault();
    }
}
